import request from '@/utils/request'

/***************************************************** 订单列表 ****************************************************/
/**
 * 获取订单设置
 */
export function getShopOrderConfig() {
    return request.get(`shop/order/config`)
}

/**
 * 获取订单状态列表
 */
export function getShopOrderStatus() {
    return request.get(`shop/order/status`)
}

/**
 * 获取订单列表
 */
export function getShopOrder(params: Record<string, any>) {
    // return request.get(`shop/order`, params)
    return request.get(`trade/order/page`, params, { isYuDao: true })

}

/**
 * 获取订单角标数据
 */
export function getShopOrderNum() {
    // return request.get(`shop/order/num`)
    return request.get(`trade/order/get-count`, {}, { isYuDao: true })


}

/**
 * 获取订单详情
 */
// export function getShopOrderDetail(order_id: any) {
//     return request.get(`shop/order/${order_id}`)

// }
export function getShopOrderDetail(params: any) {
    return request.get(`trade/order/get-detail`, params, { isYuDao: true })

}


/**
 * 关闭订单
 */
// export function orderClose(order_id: number) {
//     return request.put(`shop/order/close/${order_id}`)
// }
export function orderClose(order_id: number) {
    console.log(order_id)
    return request.delete(`trade/order/cancel?id=${order_id}`, {}, { isYuDao: true })
}

/**
 * 订单完成
 */
export function orderFinish(order_id: number) {
    // return request.put(`shop/order/finish/${order_id}`)
    return request.put(`trade/order/receive`, { id: order_id }, { isYuDao: true })

}

/**
 * 订单创建计算
 */
export function orderCreateCalculate(data: Record<string, any>) {
    // return request.post('shop/order_create/calculate', params)
    const data2 = {
        ...data,
    };
    // 移除多余字段
    if (!(data.couponId > 0)) {
        delete data2.couponId;
    }
    if (!(data.addressId > 0)) {
        delete data2.addressId;
    }
    if (!(data.pickUpStoreId > 0)) {
        delete data2.pickUpStoreId;
    }
    if (uni.$u.test.isEmpty(data.receiverName)) {
        delete data2.receiverName;
    }
    if (uni.$u.test.isEmpty(data.receiverMobile)) {
        delete data2.receiverMobile;
    }
    if (!(data.combinationActivityId > 0)) {
        delete data2.combinationActivityId;
    }
    if (!(data.combinationHeadId > 0)) {
        delete data2.combinationHeadId;
    }
    if (!(data.seckillActivityId > 0)) {
        delete data2.seckillActivityId;
    }
    if (!(data.pointActivityId > 0)) {
        delete data2.pointActivityId;
    }
    // 解决 SpringMVC 接受 List<Item> 参数的问题
    delete data2.items;
    for (let i = 0; i < data.items.length; i++) {
        data2[encodeURIComponent('items[' + i + '' + '].skuId')] = data.items[i].skuId + '';
        data2[encodeURIComponent('items[' + i + '' + '].count')] = data.items[i].count + '';
        if (data.items[i].cartId) {
            data2[encodeURIComponent('items[' + i + '' + '].cartId')] = data.items[i].cartId + '';
        }
    }
    const queryString = Object.keys(data2)
        .map((key) => key + '=' + data2[key])
        .join('&');
    return request.get(`trade/order/settlement?${queryString}`, {}, { isYuDao: true })
}

/**
 * 订单创建
 */
export function orderCreate(params: Record<string, any>) {
    // return request.post('shop/order_create/create', params)
    return request.post('trade/order/create', params, { isYuDao: true })

}

/**
 * 查询订单可用优惠券
 */
export function orderCoupon(params: Record<string, any>) {
    return request.get('shop/order_create/coupon', params)
}

/**
 * 查询自提点
 */
export function getStoreList(params: Record<string, any>) {
    // return request.get('shop/order_create/store', params)
    return request.get('trade/delivery/pick-up-store/list', params, { isYuDao: true })
}
/**
 * 查询自提点详情
 */
export function getStoreDetail(params: Record<string, any>) {
    // return request.get('shop/order_create/store', params)
    return request.get('trade/delivery/pick-up-store/get', params, { isYuDao: true })


}

/**
 * 查询物流信息
 */
export function getMaterialflowList(params: Record<string, any>) {
    return request.get('shop/order/logistics', params)
}

/**
 * 查询交易配置
 */
export function getTradeConfig(params: Record<string, any>) {
    return request.get('trade/config/get', params, { isYuDao: true })
}
